<?php

namespace app\user\controller;

class Researchresultcopy extends base
{
    public function _initialize()
    {
        $this->model = model('research_result_copy');
    }
//前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
    ];
    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();

            if (isset($data['status']) || !empty($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }

            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
        }
        $this->datas ['qid'] = $this->getLoginUser()->id;
    }


    public function read($id){
        $list = array();
        $count =null;
        $scorecount =null;
        $an = db('research_result_copy')->where('id', $id)->where('qid',$this->getLoginUser()->id)->order('id asc')->find();

        if(empty($an) || is_null($an['score'])){
            echo "<script>history.back();</script>";
            exit();
        }

        $question = db('Research_question_copy')->where(array('rid' => $an['rid']))->order('id asc')->select();
        foreach ($question as $i => $q) {
            $list[$q['id']]['question'] = $q;
            $list[$q['id']]['answer'] = null;
            $answerobj = db('Research_answer_copy')->field('name, question')->where(array('qid' => $q['id']))->order('id asc')->limit(4)->select();
            $list[$q['id']]['answerlist'] = $answerobj;
        }

        $answer = json_decode($an['content']);
        if($an['contentscore']){
            $contentscore = json_decode($an['contentscore']);
            foreach ($contentscore as $ki => $va) {
                $list[$ki]['contentscore'] = $va;
                $scorecount +=$va;
            }
        }
        foreach ($answer as $ki => $va) {
            $list[$ki]['answer'] = $va;
            $question =db('research_question_copy')->where('id',$ki)->where('answer',$va)->where('type','neq',2)->value('point');
            $count += $question;
        }

        $type =db('research_question_copy')->where('rid',$an['rid'])->where('type',2)->value('id');

        $this->assign([
            'list' => $list,
            'an' => $an,
            'count' => $count,
            'scorecount' => $scorecount,
            'type' => $type,
        ]);
        return $this->fetch();
    }
}
